package com.itzb.asone.tag;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.springframework.jdbc.core.JdbcTemplate;

import com.itzb.asone.utils.SpringContextUtil;

public class AsoneTag extends SimpleTagSupport {
	
	private static String TAG_TYPE_SELECT = "select";

	
	
	//标签类型
	private String type = "";
	//表名称
	private String table = "";
	//显示字段
	private String fieldName = "";
	//value字段
	private String fieldValue = "";
	//HTML属性内容
	private String tagAttr = "";
	//选中选项
	private String checkValue = "";
	//查询SQL条件
	private String where = "";
	//是否显示“请选择”字样
	private String top = "true";
	
	@Override
	public void doTag() throws JspException, IOException {
		String html = "";
		List<Map<String, Object>> list = getDate();
		//下拉菜单
		if(type.equals(TAG_TYPE_SELECT)){
			html = createSelect(list);
		}
		JspWriter out = this.getJspContext().getOut();
		out.write(html);
		super.doTag();
	}
	
	
	
//	=================================================================
	
	
	
	private String createSelect(List<Map<String, Object>> list){
		String html = "<select class=\"form-control select2\" " + tagAttr + "> ";
		String option = "";
		if(top.equals("true")){
			option = "<option>请选择</option>";
		}
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = list.get(i);
			if(checkValue.equals(map.get("fieldValue"))){
				option += "<option height=\"34\" selected=\"selected\" value=\"" + map.get("fieldValue") + "\">" + map.get("fieldName") + "</option>";
			} else {
				option += "<option height=\"34\" value=\"" + map.get("fieldValue") + "\">" + map.get("fieldName") + "</option>";
			}
		}
		html = (html + option + "</select>");
		return html;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	private List<Map<String, Object>> getDate(){
		JdbcTemplate jdbcTemplate = SpringContextUtil.getBean(JdbcTemplate.class);
		String sql = "select " + fieldValue + " as fieldValue," + fieldName + " as fieldName from asone." + table;
		if(!where.equals("")){
			sql += " where " + where;
		}
		List<Map<String, Object>> result = jdbcTemplate.queryForList(sql);
		return result;
	}
	
	
	
//	=================================================================

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getTable() {
		return table;
	}

	public void setTable(String table) {
		this.table = table;
	}

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public String getFieldValue() {
		return fieldValue;
	}

	public void setFieldValue(String fieldValue) {
		this.fieldValue = fieldValue;
	}

	public String getTagAttr() {
		return tagAttr;
	}

	public void setTagAttr(String tagAttr) {
		this.tagAttr = tagAttr;
	}

	public String getCheckValue() {
		return checkValue;
	}

	public void setCheckValue(String checkValue) {
		this.checkValue = checkValue;
	}

	public String getWhere() {
		return where;
	}

	public void setWhere(String where) {
		this.where = where;
	}

	public String getTop() {
		return top;
	}

	public void setTop(String top) {
		this.top = top;
	}

}
